package com.usian.user.controller;


import com.usian.common.Result;
import com.usian.common.ResultCodeEnum;
import com.usian.entity.Cars;
import com.usian.user.config.UploadFile;
import com.usian.entity.Users;
import com.usian.user.service.IUsersService;
import com.usian.vo.RentalDetailsVo;
import com.usian.vo.ReserveVo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/user/users")
public class UsersController {

    @Autowired
    private IUsersService usersService;

    @Operation(summary = "获取验证码")
    @GetMapping("/getCode/{phone}")
    public Result<String> getCode(@PathVariable String phone){
        return usersService.getCode(phone);
    }
    @Operation(summary = "用户登录")
    @GetMapping("/login/{phone}/{code}")
    public Result<Users> login(@PathVariable String phone, @PathVariable String code){
        return usersService.login(phone,code);
    }

    @Operation(summary = "根据id查询用户信息")
    @GetMapping("/getById/{id}")
    public Result<Users> getById(@PathVariable Integer id){
        Users users = usersService.getById(id);
        if (users != null){
            return Result.build(users, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null, ResultCodeEnum.DATA_NOT_EXISTS);
    }

    @Operation(summary = "图片上传")
    @PostMapping("/upload/uploadLocal")
    public Result<String> uploadLocal(@RequestParam("file") MultipartFile file) throws Exception{
        String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String filepath = UUID.randomUUID().toString();
        System.out.println(filepath+fileName);
        return Result.build(UploadFile.uploadByBytes(file.getBytes(),filepath+fileName), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改用户信息")
    @PutMapping("/update")
    public Result<Users> update(@RequestBody Users users){
        boolean update = usersService.updateById(users);
        if (update){
            return Result.build(users, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null, ResultCodeEnum.DATA_NOT_EXISTS);
    }

    @Operation(summary = "我的租聘详情")
    @GetMapping("/rentalDetails/{userId}")
    public Result<RentalDetailsVo> rentalDetails(@PathVariable Integer userId){
        return usersService.rentalDetails(userId);
    }

    @Operation(summary = "查询已预定的信息")
    @GetMapping("/findAllBooked/{userId}")
    public Result<List<ReserveVo>> findAllBooked(@PathVariable Integer userId){
        //查询已经预定所有订单
        return Result.build(usersService.findAllBooked(userId), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询已预定的信息")
    @GetMapping("/findAllzhifu/{userId}")
    public Result<List<ReserveVo>> findAllzhifu(@PathVariable Integer userId){
        //查询已经预定所有订单
        return Result.build(usersService.findAllzhifu(userId), ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "查询历史记录")
    @GetMapping("/findAllHistory/{userId}")
    public Result<List<ReserveVo>> findAllHistory(@PathVariable Integer userId){
        //查询已经预定所有订单
        return Result.build(usersService.findAllHistory(userId), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询所有订单")
    @GetMapping("/findAllList/{userId}")
    public Result<List<ReserveVo>> findAllList(@PathVariable Integer userId){
        //查询所有订单
        return Result.build(usersService.findAllList(userId), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询用户预定车辆的信息")
    @GetMapping("/findReservedCar/{userId}/{carId}")
    public Result<ReserveVo> findReservedCar(@PathVariable Integer userId, @PathVariable Integer carId){
        return Result.build(usersService.findReservedCar(userId,carId), ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "查询用户支付车辆的信息")
    @GetMapping("/findzhifuCar/{userId}/{carId}")
    public Result<ReserveVo> findzhifuCar(@PathVariable Integer userId, @PathVariable Integer carId){
        return Result.build(usersService.findzhifuCar(userId,carId), ResultCodeEnum.SUCCESS);
    }


}
